import Mock from "mockjs"
// 内存模拟数据
let arr = [{
  id: Mock.mock("@id"),
  name: '吃饭',
  isDone: true
},{
  id: Mock.mock("@id"),
  name: '睡觉',
  isDone: false
}]

export default [
  {
    url: "/list",
    method: "get",
    response: () => {
      return arr
    },
  },
  {
    url: "/del/:id",
    method: "delete",
    response: (req) => {
      const index = arr.findIndex((item) => item.id === req.query.id)
      if (index > -1) {
        arr.splice(index, 1)
        return { success: true }
      } else {
        return { success: false }
      }
    },
  },
  {
    url: "/deldone",
    method: "delete",
    response: (req) => {

      arr = arr.filter(it => !it.isDone)
      return { success: true }
    },
  },
  {
    url: "/edit/:id",
    method: "patch",
    response: ({ query, body }) => {
      const item = arr.find((item) => item.id === query.id)
      if (item) {
        item.name = body.name
        item.isDone = body.isDone
        return { success: true }
      } else {
        return { success: false }
      }
    },
  },
  {
    url: "/add",
    method: "post",
    response: ({ query, body }) => {
      arr.push({
        id: Mock.mock("@id"),
        name: body.name,
        isDone: body.isDone
      })
      return { success: true }
 
    },
  }
]
